﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RemoteDesktopClient
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            //IPHostEntry iphost;
            //iphost = Dns.GetHostEntry(Dns.GetHostName());
            //lblClientIP.Text = iphost.AddressList.
            //    FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();

            foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || 
                    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    IPInterfaceProperties ipProps = netInterface.GetIPProperties();
                    foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
                    {
                        if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            txtClientIP.AppendText("Name: " + netInterface.Name + "\n");
                            txtClientIP.AppendText("IP: " + addr.Address.ToString() + "\n\n");
                            txtClientIP.AppendText("------------------------------------------------------------------- \n");
                        }

                    }
                }
            }


        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if(txtServerIP.Text.Equals(""))
            {
                MessageBox.Show("Bạn chưa nhập IP của server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
                
            using (var frmRemoteDesktop = new Form1()) {
                frmRemoteDesktop.FormClosing += (s, ea) => this.Show();

                frmRemoteDesktop.ConfigServerService(txtServerIP.Text);

                this.Hide();
                if (frmRemoteDesktop.ShowDialog() == DialogResult.OK)
                {
                    
                }
            }
        }

        private void txtServerIP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46 || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

    }
}
